package cn.com.callback

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import cn.com.elite.EliteBean;
import cn.com.elite.PropertiesUtil;
import cn.com.elite.Util;
import java.util.Map;
import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
import net.sf.json.JSONArray;
import java.net.URLDecoder;
import java.net.URLEncoder;

public class SmsGetMoWxtl implements CallBackInvoke{
	private static final Log log=LogFactory.getLog(SmsGetMoWxtl.class);
	public void doCallBack(EliteBean eliteBean,String callBack,Map map,String body){
		log.debug("=========================WXTLGetMo==================");
		PropertiesUtil pt= new PropertiesUtil();
		def timestamp = Util.getNowString("MMddHHmmss");
		def userid = Base64.encode(URLEncoder.encode(pt.readValue("wxtl_userid"),"utf-8"),"utf-8"); //短信账号 先URLEncode编码后使用Base64算法加密 
		def pwd = Base64.encode(URLEncoder.encode(pt.readValue("wxtl_pwd"),"utf-8"),"utf-8"); //短信密码  先URLEncode编码后使用Base64算法加密
		def ipport = pt.readValue("sms_ip")+":"+pt.readValue("wxtl_port"); //短信通道ip+端口
		def cnt = 100;
		def url = "http://"+ipport+"/communication/fetchDelivers.ashx?cid="+userid+"&pwd="+pwd+"&cnt="+cnt; //接口请求地址
		log.debug("url:"+url);
		def charset = "utf-8"; //设置字符集
		def timeout = 100000; //设置请求超时时间
		def enableSSLflag = 0;
		Map headMap=new HashMap(); //设置头文件
		headMap.put("Content-Type","application/json");
		def i = 0;
		def mobile = "";
		def spno = 0;
		def exno = 0;
		def msg = "";
		try {				
			def msgjson = Util.doHttpGet(url,charset,timeout,enableSSLflag);
			//log.debug("msgjson:"+msgjson);
			JSONObject obj = JSONObject.fromObject(msgjson);
			if(obj.get("status").equals("0")) {
				JSONArray data = obj.getJSONArray("data");
				for (i = 0; i < data.size(); i++) {
					try {
						JSONObject it = data.get(i);
						//log.debug("it:"+it);
						mobile = URLDecoder.decode(Base64.decode(it.get("mobile"),"utf-8"),"UTF-8");
						spno = Util.filterForObject(it.get("ssid")).equals("")?0:it.get("ssid");
						msg = URLDecoder.decode(Base64.decode(it.get("content"),"utf-8"),"UTF-8");
						eliteBean.update("insert into sms_getmo(rec_guid,mobile,spno,rtime,msg,pipe)values(sysguid,'"+mobile+"','"+spno+"',sysdate,'"+msg+"',3)","dataSource");
						/**
						if(msg.equals("T") || msg.equals("01") || msg.equals("02") || msg.equals("03")) {
							smap = eliteBean.chaxun_sql("select sk_invpty,invpty_name,sms_id from sms_send where phone='"+mobile+"' and exno="+exno+"","dataSource");
							if(smap.size()==0) {
								continue;
							};
						}else{
							continue;
						};
						def sms_id = smap[0].get("sms_id");
						//回复了具体订阅ID，如果下行不是开户上行短信模板的，则终止本次循环
						if(!sms_id.equals("SMS011")&&(msg.equals("01") || msg.equals("02") || msg.equals("03"))) {
							continue;
						};
						input = new String[5];
						input[0] = smap[0].get("sk_invpty");
						input[1] = msg;
						input[2] = smap[0].get("sms_id");
						input[3] = mobile;
						input[4] = smap[0].get("invpty_name");
						sqlresult = eliteBean.executeWithReturn("{call sp_sms_smssubscription(?,?,?,?,?,?)}",input,"dataSource");
						*/
					}catch(Exception e) {
						log.error("ErrorMessage:",e);
					};
				};
			};
		}catch(Exception e) {
			log.error("ErrorMessage:",e);
		};
	}
}
